package com.aletto.general_store.presenters;

import android.app.Activity;
import android.view.ViewGroup;
import android.widget.EditText;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.ToastView;
import com.sisyphus.spinach.ui.configs.impl.DefaultTemplate;


public class AddAddressTemplate extends DefaultTemplate<Object> {

    public AddAddressTemplate(ViewGroup parent) {
        super(parent);
    }

    @Override
    public INetTask.View<Object> view() {
        return new ToastView<>(mParent.getContext()){
            @Override
            public void complete() {
                super.complete();
                ((Activity) mParent.getContext()).finish();
            }
        };
    }

    @Override
    public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
        EditText nameEdit = mParent.findViewById(R.id.name_edit);
        EditText phoneEdit = mParent.findViewById(R.id.phone_edit);
        EditText addressDetailsEdit = mParent.findViewById(R.id.address_details_edit);

        return new NetSingleTask<>(
                DataRepository.getInstance()
                        .addAddress(
                                nameEdit.getText().toString(),
                                phoneEdit.getText().toString(),
                                addressDetailsEdit.getText().toString()
                        )
        );
    }

}
